package jnpf.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jnpf.base.service.BillRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.base.UserInfo;
import jnpf.base.vo.DownloadVO;
import jnpf.config.ConfigValueUtil;
import jnpf.base.util.VisualUtils;
import jnpf.exception.DataException;

import org.springframework.transaction.annotation.Transactional;
import jnpf.base.entity.ProvinceEntity;
import jnpf.engine.entity.FlowTaskEntity;
import jnpf.engine.enums.FlowStatusEnum;
import jnpf.engine.service.FlowTaskService;
import jnpf.exception.WorkFlowException;
import jnpf.model.zxgl_renwufankuixieban.*;
import jnpf.model.zxgl_renwufankuixieban.ZXGL_RenWuFanKuiXieBanPagination;
import jnpf.entity.*;
import jnpf.base.service.ProvinceService;
import jnpf.base.service.VisualdevService;
import jnpf.entity.ZXGL_RenWuFanKuiJiaoFuWuEntity;
import jnpf.util.*;
import jnpf.base.util.*;
import jnpf.base.vo.ListVO;
import jnpf.util.context.SpringContext;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import jnpf.generater.util.GeneraterSwapUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import jnpf.base.entity.VisualdevEntity;
import jnpf.entity.ZXGL_RenWuFanKuiXieBanEntity;
import jnpf.service.ZXGL_RenWuFanKuiXieBanService;
import jnpf.entity.ZXGL_RenWuFanKuiJiaoFuWuEntity;
import jnpf.service.ZXGL_RenWuFanKuiJiaoFuWuService;
import jnpf.generater.util.GeneraterSwapUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

import jnpf.file.FileApi;
import jnpf.model.upload.UploadFileModel;
import jnpf.emnus.FileTypeEnum;

import java.io.ByteArrayOutputStream;

import jnpf.provider.file.FileUploadProvider;
import org.apache.dubbo.config.annotation.DubboReference;


/**
 * ZXGL_RenWuFanKuiXieBan
 *
 * @版本： V2.0
 * @版权： 河北交投智能交通技术有限责任公司
 * @作者： 数字交投项目组
 * @日期： 2025-03-20
 */
@Slf4j
@RestController
@Api(tags = "ZXGL_RenWuFanKuiXieBan", value = "zxgl")
@RequestMapping("/ZXGL_RenWuFanKuiXieBan")
public class ZXGL_RenWuFanKuiXieBanController {


    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;
    @Autowired
    private UserProvider userProvider;

    @Autowired
    private VisualdevService visualdevService;

    @Autowired
    private FlowTaskService flowTaskService;

    @Autowired
    private ZXGL_RenWuFanKuiXieBanService zXGL_RenWuFanKuiXieBanService;

    @Autowired
    private ZXGL_RenWuFanKuiJiaoFuWuService zXGL_RenWuFanKuiJiaoFuWuService;


    /**
     * 列表
     *
     * @param zXGL_RenWuFanKuiXieBanPagination
     * @return
     */
    @PostMapping("/getList")
    public ActionResult list(@RequestBody ZXGL_RenWuFanKuiXieBanPagination zXGL_RenWuFanKuiXieBanPagination) throws IOException {
        List<ZXGL_RenWuFanKuiXieBanEntity> list = zXGL_RenWuFanKuiXieBanService.getList(zXGL_RenWuFanKuiXieBanPagination);
        //处理id字段转名称，若无需转或者为空可删除
        for (ZXGL_RenWuFanKuiXieBanEntity entity : list) {
            entity.setFkr(generaterSwapUtil.userSelectValues(entity.getFkr()));
            entity.setFkbm(generaterSwapUtil.comSelectValues(entity.getFkbm()));
        }
        List<ZXGL_RenWuFanKuiXieBanListVO> listVO = JsonUtil.getJsonToList(list, ZXGL_RenWuFanKuiXieBanListVO.class);
        for (ZXGL_RenWuFanKuiXieBanListVO zXGL_RenWuFanKuiXieBanVO : listVO) {
        }


        for (ZXGL_RenWuFanKuiXieBanListVO vo : listVO) {
            FlowTaskEntity flowTaskEntity = flowTaskService.getInfoSubmit(vo.getId());
            if (flowTaskEntity != null) {
                vo.setFlowState(flowTaskEntity.getStatus());
            } else {
                vo.setFlowState(null);
            }
        }
        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = JsonUtil.getJsonToBean(zXGL_RenWuFanKuiXieBanPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);
    }


    /**
     * 创建
     *
     * @param zXGL_RenWuFanKuiXieBanCrForm
     * @return
     */
    @PostMapping
    @Transactional
    public ActionResult create(@RequestBody @Valid ZXGL_RenWuFanKuiXieBanCrForm zXGL_RenWuFanKuiXieBanCrForm) throws DataException {
        String visualId = "671704747970950661";
        VisualdevEntity visualdevEntity = visualdevService.getInfo(visualId);
        String mainId = RandomUtil.uuId();
        UserInfo userInfo = userProvider.get();
        ZXGL_RenWuFanKuiXieBanEntity entity = JsonUtil.getJsonToBean(zXGL_RenWuFanKuiXieBanCrForm, ZXGL_RenWuFanKuiXieBanEntity.class);
        entity.setId(mainId);
        entity.setCreatorTime(new Date());
        zXGL_RenWuFanKuiXieBanService.save(entity);
        List<ZXGL_RenWuFanKuiJiaoFuWuEntity> ZXGL_RenWuFanKuiJiaoFuWuList = JsonUtil.getJsonToList(zXGL_RenWuFanKuiXieBanCrForm.getZxgl_renwufankuijiaofuwuList(), ZXGL_RenWuFanKuiJiaoFuWuEntity.class);

        for (ZXGL_RenWuFanKuiJiaoFuWuEntity entitys : ZXGL_RenWuFanKuiJiaoFuWuList) {
            entitys.setId(RandomUtil.uuId());
            entitys.setId(entity.getMainid());
            zXGL_RenWuFanKuiJiaoFuWuService.save(entitys);
        }
        if (FlowStatusEnum.submit.getMessage().equals(zXGL_RenWuFanKuiXieBanCrForm.getStatus())) {
            try {
                VisualUtils.submitFlowTask(visualdevEntity, mainId, zXGL_RenWuFanKuiXieBanCrForm, userInfo, zXGL_RenWuFanKuiXieBanCrForm.getCandidateList());
            } catch (WorkFlowException e) {
                e.printStackTrace();
            }
            return ActionResult.success("提交成功，请耐心等待");
        }
        return ActionResult.success("保存成功");
    }


    /**
     * 信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ActionResult<ZXGL_RenWuFanKuiXieBanInfoVO> info(@PathVariable("id") String id) {
        ZXGL_RenWuFanKuiXieBanEntity entity = zXGL_RenWuFanKuiXieBanService.getInfo(id);
        ZXGL_RenWuFanKuiXieBanInfoVO vo = JsonUtil.getJsonToBean(entity, ZXGL_RenWuFanKuiXieBanInfoVO.class);

        //子表
        List<ZXGL_RenWuFanKuiJiaoFuWuEntity> ZXGL_RenWuFanKuiJiaoFuWuList = zXGL_RenWuFanKuiXieBanService.GetZXGL_RenWuFanKuiJiaoFuWuList(id);
        for (ZXGL_RenWuFanKuiJiaoFuWuEntity zxgl_renwufankuijiaofuwuEntity : ZXGL_RenWuFanKuiJiaoFuWuList) {

        }
        vo.setZXGL_RenWuFanKuiJiaoFuWuList(JsonUtil.getJsonToList(ZXGL_RenWuFanKuiJiaoFuWuList, ZXGL_RenWuFanKuiJiaoFuWuModel.class));
        //副表
        return ActionResult.success(vo);
    }


    /**
     * 更新
     *
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    @Transactional
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid ZXGL_RenWuFanKuiXieBanUpForm zXGL_RenWuFanKuiXieBanUpForm) throws DataException {
        String visualId = "671704747970950661";
        VisualdevEntity visualdevEntity = visualdevService.getInfo(visualId);
        UserInfo userInfo = userProvider.get();
        ZXGL_RenWuFanKuiXieBanEntity entity = zXGL_RenWuFanKuiXieBanService.getInfo(id);
        if (entity != null) {
            ZXGL_RenWuFanKuiXieBanEntity subentity = JsonUtil.getJsonToBean(zXGL_RenWuFanKuiXieBanUpForm, ZXGL_RenWuFanKuiXieBanEntity.class);
            zXGL_RenWuFanKuiXieBanService.update(id, subentity);

            QueryWrapper<ZXGL_RenWuFanKuiJiaoFuWuEntity> ZXGL_RenWuFanKuiJiaoFuWuqueryWrapper = new QueryWrapper<>();
            ZXGL_RenWuFanKuiJiaoFuWuqueryWrapper.lambda().eq(ZXGL_RenWuFanKuiJiaoFuWuEntity::getId, entity.getMainid());
            zXGL_RenWuFanKuiJiaoFuWuService.remove(ZXGL_RenWuFanKuiJiaoFuWuqueryWrapper);

            List<ZXGL_RenWuFanKuiJiaoFuWuEntity> ZXGL_RenWuFanKuiJiaoFuWuList = JsonUtil.getJsonToList(zXGL_RenWuFanKuiXieBanUpForm.getZxgl_renwufankuijiaofuwuList(), ZXGL_RenWuFanKuiJiaoFuWuEntity.class);
            for (ZXGL_RenWuFanKuiJiaoFuWuEntity entitys : ZXGL_RenWuFanKuiJiaoFuWuList) {
                entitys.setId(RandomUtil.uuId());
                entitys.setId(entity.getMainid());
                zXGL_RenWuFanKuiJiaoFuWuService.save(entitys);
            }


            if (FlowStatusEnum.submit.getMessage().equals(zXGL_RenWuFanKuiXieBanUpForm.getStatus())) {
                try {
                    VisualUtils.submitFlowTask(visualdevEntity, id, subentity, userInfo, zXGL_RenWuFanKuiXieBanUpForm.getCandidateList());
                } catch (WorkFlowException e) {
                    e.printStackTrace();
                }
                return ActionResult.success("提交成功，请耐心等待");
            }
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        ZXGL_RenWuFanKuiXieBanEntity entity = zXGL_RenWuFanKuiXieBanService.getInfo(id);
        if (entity != null) {
            FlowTaskEntity taskEntity = flowTaskService.getInfoSubmit(id);
            if (taskEntity != null) {
                try {
                    flowTaskService.delete(taskEntity);
                } catch (WorkFlowException e) {
                    e.printStackTrace();
                }
            }
            zXGL_RenWuFanKuiXieBanService.delete(entity);
            QueryWrapper<ZXGL_RenWuFanKuiJiaoFuWuEntity> queryWrapperZXGL_RenWuFanKuiJiaoFuWu = new QueryWrapper<>();
            queryWrapperZXGL_RenWuFanKuiJiaoFuWu.lambda().eq(ZXGL_RenWuFanKuiJiaoFuWuEntity::getId, entity.getMainid());
            zXGL_RenWuFanKuiJiaoFuWuService.remove(queryWrapperZXGL_RenWuFanKuiJiaoFuWu);

        }
        return ActionResult.success("删除成功");
    }


}
